<?php
use common\models\Game;
?>

<div class="me_shadow public womargin">
    <div class="tit"><p class="p4"><?=$type['type_name'];?></p></div>
    <div class="hot_game">
        <ul class="me_shadow" id="game_list">
            <?php foreach($games as $hot):?>
            <li><a href="<?=Yii::$app->urlManager->createUrl(['/game/detail','version_id'=>$hot['id']]);?>">
               <p class="p1"><img src="<?=filepath($hot['game']['game_logo']);?>" class="myimg"></p>
               <p class="p2">
                  <span class="span1"><?=$hot['game']['game_name']?></span>
                  <span class="span2"><?=$hot['version_size'].'MB';?> <br>
                  <i>
                    <?php if(!empty($hot['game']['types'])):?>
                        <?php foreach ($hot['game']['types'] as $key=>$type):?>
				            <?php echo $type['type_name'].' ';?>
				        <?php endforeach;?>
                    <?php endif;?>
                  </i>
                                   下载：<em><?=$hot['game']['game_downclick'];?></em></span>
               </p>
               <p class="p3"><span>下载</span></p>
            </a></li>
            <?php endforeach;?>
        </ul>
    </div>
</div>
<?php if($limit<=count($games)):?>
<div class="loadgame" data-page="2"><p>加载更多游戏 </p></div>
<input type="hidden" name="type_id" id="type_id" value="<?=$type['type_id'];?>" />
<?php endif;?>

<?php $this->beginBlock('script'); ?>
<script type="text/javascript">
//点击加载更多时，瀑布流实现
$(".loadgame").click(function() {
    getGames();
});

//将时间戳转化为2015-09-09这样格式的日期 by jois add at 20150911
function formatDate(date){
	date=new Date(parseInt(date)*1000).toLocaleDateString();
	return date.replace(/\//g, '-').replace(/-(\d)?/, "-0$1");;
}

//加载更多函数
function getGames(){
	//初始化参数
	var add = $(".loadgame");
	var type_id=$("#type_id").val();
	var page = parseInt(add.attr('data-page'));
	if (isNaN(page) || page < 2) {
        page = 2;
    }

    //防止恶意点击
	if (add.attr('data-action') == 1) {
        return false;
    }
	add.attr('data-action', 1);
    add.find('p').text('获取中...');

    //ajax请求数据
    $.ajax({
        type: 'GET',
        url: "<?=Yii::$app->urlManager->createUrl('/game/ajax-list')?>",
        data: { page: page, type_id: type_id },
        dataType: 'json',
        success: function(data) {
            if (data.length < 1) {
                add.hide();
            } else {
            	var html = "";
                var file_path1="<?=\Yii::$app->params['filePath'];?>";
                var file_path2="<?=\Yii::$app->params['pipawImgPath'];?>";
                var tmp="<?=Yii::$app->urlManager->createUrl('/game/detail');?>";
                for (var i in data) {
                    if(data[i]['game']['game_logo'].substr(1,1)=='/'){
                    	var img_path=file_path1+data[i]['game']['game_logo'];
                    }else{
                    	var img_path=file_path2+data[i]['game']['game_logo'];
                    }
                    var type=data[i]['game']['types'];
                    html+='<li><a href="'+tmp+'?version_id='+data[i]['id']+'"><p class="p1"><img src="'+img_path+'" class="myimg"></p>' +
                	'<p class="p2"><span class="span1">'+data[i]['game']['game_name']+'</span><span class="span2">'+data[i]['version_size']+'MB<br>';
                    //一个游戏可能多个类型，遍历类型
                	if(type){
             		   html+='<i>';
               		   for(var j in type){
                  			html+=type[j]['type_name']+' ';
                   	   }
                  		html+='</i>';
                    }else{
                       html+='<i></i>';
                    }
                    html+='下载：<em>'+data[i]['game']['game_downclick']+'</em></span></p><p class="p3"><span>下载</span></p></a></li>';
                }
                $("#game_list").append(html);
                add.attr('data-page', parseInt(add.attr('data-page'))+1);
            }
            add.attr('data-action', 0);
            add.find('p').text('加载更多游戏');
        },
        error: function(xhq) {
        	alert(xhq.responseText);
            add.attr('data-action', 0);
            add.find('p').text('加载更多游戏');
        }
    });
}
</script>
<?php $this->endBlock();?>
